#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e6+10,MOD=1e9+7;
LL n,m,jc[MAXN]={1},f[MAXN]={1,0};
LL qpow(LL a,LL b)
{
	LL ans=1;
	for(;b;b>>=1,a=a*a%MOD)
		if(b&1)ans=ans*a%MOD;
	return ans;
}
int main()
{
	for(LL i=1;i<=MAXN;++i)jc[i]=jc[i-1]*i%MOD;
	for(LL i=2;i<=MAXN;++i)f[i]=(i*f[i-1]%MOD+((i-2)%2==1?-1:1))%MOD;
	LL T;
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&m);
		LL ans=jc[n];
		ans=ans*qpow(jc[m],MOD-2)%MOD;
		ans=ans*qpow(jc[n-m],MOD-2)%MOD;
		printf("%lld\n",ans*f[n-m]%MOD);
	}
	return 0;
}
